<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>anchoredviewportposition.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_positioning_anchoredviewportposition.js.html">anchoredviewportposition.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Anchored viewport positioning class.
<a name="line17"></a> *
<a name="line18"></a> * @author eae@google.com (Emil A Eklund)
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.positioning.AnchoredViewportPosition&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.math.Box&#39;);
<a name="line24"></a>goog.require(&#39;goog.positioning&#39;);
<a name="line25"></a>goog.require(&#39;goog.positioning.AnchoredPosition&#39;);
<a name="line26"></a>goog.require(&#39;goog.positioning.Corner&#39;);
<a name="line27"></a>goog.require(&#39;goog.positioning.Overflow&#39;);
<a name="line28"></a>goog.require(&#39;goog.positioning.OverflowStatus&#39;);
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>/**
<a name="line33"></a> * Encapsulates a popup position where the popup is anchored at a corner of
<a name="line34"></a> * an element. The corners are swapped if dictated by the viewport. For instance
<a name="line35"></a> * if a popup is anchored with its top left corner to the bottom left corner of
<a name="line36"></a> * the anchor the popup is either displayed below the anchor (as specified) or
<a name="line37"></a> * above it if there&#39;s not enough room to display it below.
<a name="line38"></a> *
<a name="line39"></a> * When using this positioning object it&#39;s recommended that the movable element
<a name="line40"></a> * be absolutely positioned.
<a name="line41"></a> *
<a name="line42"></a> * @param {Element} anchorElement Element the movable element should be
<a name="line43"></a> *     anchored against.
<a name="line44"></a> * @param {goog.positioning.Corner} corner Corner of anchored element the
<a name="line45"></a> *     movable element should be positioned at.
<a name="line46"></a> * @param {boolean=} opt_adjust Whether the positioning should be adjusted until
<a name="line47"></a> *     the element fits inside the viewport even if that means that the anchored
<a name="line48"></a> *     corners are ignored.
<a name="line49"></a> * @param {goog.math.Box=} opt_overflowConstraint Box object describing the
<a name="line50"></a> *     dimensions in which the movable element could be shown.
<a name="line51"></a> * @constructor
<a name="line52"></a> * @extends {goog.positioning.AnchoredPosition}
<a name="line53"></a> */
<a name="line54"></a>goog.positioning.AnchoredViewportPosition = function(anchorElement,
<a name="line55"></a>                                                     corner,
<a name="line56"></a>                                                     opt_adjust,
<a name="line57"></a>                                                     opt_overflowConstraint) {
<a name="line58"></a>  goog.positioning.AnchoredPosition.call(this, anchorElement, corner);
<a name="line59"></a>
<a name="line60"></a>  /**
<a name="line61"></a>   * The last resort algorithm to use if the algorithm can&#39;t fit inside
<a name="line62"></a>   * the viewport.
<a name="line63"></a>   *
<a name="line64"></a>   * IGNORE = do nothing, just display at the preferred position.
<a name="line65"></a>   *
<a name="line66"></a>   * ADJUST_X | ADJUST_Y = Adjust until the element fits, even if that means
<a name="line67"></a>   * that the anchored corners are ignored.
<a name="line68"></a>   *
<a name="line69"></a>   * @type {number}
<a name="line70"></a>   * @private
<a name="line71"></a>   */
<a name="line72"></a>  this.lastResortOverflow_ = opt_adjust ?
<a name="line73"></a>      (goog.positioning.Overflow.ADJUST_X |
<a name="line74"></a>       goog.positioning.Overflow.ADJUST_Y) :
<a name="line75"></a>      goog.positioning.Overflow.IGNORE;
<a name="line76"></a>
<a name="line77"></a>  /**
<a name="line78"></a>   * The dimensions in which the movable element could be shown.
<a name="line79"></a>   * @type {goog.math.Box|undefined}
<a name="line80"></a>   * @private
<a name="line81"></a>   */
<a name="line82"></a>  this.overflowConstraint_ = opt_overflowConstraint || undefined;
<a name="line83"></a>};
<a name="line84"></a>goog.inherits(goog.positioning.AnchoredViewportPosition,
<a name="line85"></a>              goog.positioning.AnchoredPosition);
<a name="line86"></a>
<a name="line87"></a>
<a name="line88"></a>/**
<a name="line89"></a> * @return {goog.math.Box|undefined} The box object describing the
<a name="line90"></a> *     dimensions in which the movable element will be shown.
<a name="line91"></a> */
<a name="line92"></a>goog.positioning.AnchoredViewportPosition.prototype.getOverflowConstraint =
<a name="line93"></a>    function() {
<a name="line94"></a>  return this.overflowConstraint_;
<a name="line95"></a>};
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * @param {goog.math.Box|undefined} overflowConstraint Box object describing the
<a name="line100"></a> *     dimensions in which the movable element could be shown.
<a name="line101"></a> */
<a name="line102"></a>goog.positioning.AnchoredViewportPosition.prototype.setOverflowConstraint =
<a name="line103"></a>    function(overflowConstraint) {
<a name="line104"></a>  this.overflowConstraint_ = overflowConstraint;
<a name="line105"></a>};
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>/**
<a name="line109"></a> * @return {number} A bitmask for the &quot;last resort&quot; overflow.
<a name="line110"></a> */
<a name="line111"></a>goog.positioning.AnchoredViewportPosition.prototype.getLastResortOverflow =
<a name="line112"></a>    function() {
<a name="line113"></a>  return this.lastResortOverflow_;
<a name="line114"></a>};
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * @param {number} lastResortOverflow A bitmask for the &quot;last resort&quot; overflow,
<a name="line119"></a> *     if we fail to fit the element on-screen.
<a name="line120"></a> */
<a name="line121"></a>goog.positioning.AnchoredViewportPosition.prototype.setLastResortOverflow =
<a name="line122"></a>    function(lastResortOverflow) {
<a name="line123"></a>  this.lastResortOverflow_ = lastResortOverflow;
<a name="line124"></a>};
<a name="line125"></a>
<a name="line126"></a>
<a name="line127"></a>/**
<a name="line128"></a> * Repositions the movable element.
<a name="line129"></a> *
<a name="line130"></a> * @param {Element} movableElement Element to position.
<a name="line131"></a> * @param {goog.positioning.Corner} movableCorner Corner of the movable element
<a name="line132"></a> *     that should be positioned adjacent to the anchored element.
<a name="line133"></a> * @param {goog.math.Box=} opt_margin A margin specified in pixels.
<a name="line134"></a> * @param {goog.math.Size=} opt_preferredSize The preferred size of the
<a name="line135"></a> *     movableElement.
<a name="line136"></a> * @override
<a name="line137"></a> */
<a name="line138"></a>goog.positioning.AnchoredViewportPosition.prototype.reposition = function(
<a name="line139"></a>    movableElement, movableCorner, opt_margin, opt_preferredSize) {
<a name="line140"></a>  var status = goog.positioning.positionAtAnchor(this.element, this.corner,
<a name="line141"></a>      movableElement, movableCorner, null, opt_margin,
<a name="line142"></a>      goog.positioning.Overflow.FAIL_X | goog.positioning.Overflow.FAIL_Y,
<a name="line143"></a>      opt_preferredSize, this.overflowConstraint_);
<a name="line144"></a>
<a name="line145"></a>  // If the desired position is outside the viewport try mirroring the corners
<a name="line146"></a>  // horizontally or vertically.
<a name="line147"></a>  if (status &amp; goog.positioning.OverflowStatus.FAILED) {
<a name="line148"></a>    var cornerFallback = this.adjustCorner(status, this.corner);
<a name="line149"></a>    var movableCornerFallback = this.adjustCorner(status, movableCorner);
<a name="line150"></a>
<a name="line151"></a>    status = goog.positioning.positionAtAnchor(this.element, cornerFallback,
<a name="line152"></a>        movableElement, movableCornerFallback, null, opt_margin,
<a name="line153"></a>        goog.positioning.Overflow.FAIL_X | goog.positioning.Overflow.FAIL_Y,
<a name="line154"></a>        opt_preferredSize, this.overflowConstraint_);
<a name="line155"></a>
<a name="line156"></a>    if (status &amp; goog.positioning.OverflowStatus.FAILED) {
<a name="line157"></a>      // If that also fails, pick the best corner from the two tries,
<a name="line158"></a>      // and adjust the position until it fits.
<a name="line159"></a>      cornerFallback = this.adjustCorner(status, cornerFallback);
<a name="line160"></a>      movableCornerFallback = this.adjustCorner(
<a name="line161"></a>          status, movableCornerFallback);
<a name="line162"></a>
<a name="line163"></a>      goog.positioning.positionAtAnchor(this.element, cornerFallback,
<a name="line164"></a>          movableElement, movableCornerFallback, null, opt_margin,
<a name="line165"></a>          this.getLastResortOverflow(), opt_preferredSize,
<a name="line166"></a>          this.overflowConstraint_);
<a name="line167"></a>    }
<a name="line168"></a>  }
<a name="line169"></a>};
<a name="line170"></a>
<a name="line171"></a>
<a name="line172"></a>/**
<a name="line173"></a> * Adjusts the corner if X or Y positioning failed.
<a name="line174"></a> * @param {number} status The status of the last positionAtAnchor call.
<a name="line175"></a> * @param {goog.positioning.Corner} corner The corner to adjust.
<a name="line176"></a> * @return {goog.positioning.Corner} The adjusted corner.
<a name="line177"></a> * @protected
<a name="line178"></a> */
<a name="line179"></a>goog.positioning.AnchoredViewportPosition.prototype.adjustCorner = function(
<a name="line180"></a>    status, corner) {
<a name="line181"></a>  if (status &amp; goog.positioning.OverflowStatus.FAILED_HORIZONTAL) {
<a name="line182"></a>    corner = goog.positioning.flipCornerHorizontal(corner);
<a name="line183"></a>  }
<a name="line184"></a>
<a name="line185"></a>  if (status &amp; goog.positioning.OverflowStatus.FAILED_VERTICAL) {
<a name="line186"></a>    corner = goog.positioning.flipCornerVertical(corner);
<a name="line187"></a>  }
<a name="line188"></a>
<a name="line189"></a>  return corner;
<a name="line190"></a>};
</pre>


</body>
</html>
